package com.chefu.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chefu.common.exception.CfException;
import com.chefu.common.exception.ExceptionEnum;
import com.chefu.common.pojo.PageResult;
import com.chefu.system.mapper.EmpMapper;
import com.chefu.system.mapper.RoleMapper;
import com.chefu.system.pojo.Dept;
import com.chefu.system.mapper.DeptMapper;
import com.chefu.system.pojo.Dept;
import com.chefu.system.pojo.Emp;
import com.chefu.system.service.DeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.el.lang.ELArithmetic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@Service
@Transactional
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService  {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;


    @Override
    public PageResult<Dept> findByPage(Integer page, Integer pageSize, String deptNum, String deptName) {

        IPage iPage = new Page(page, pageSize);

        QueryWrapper queryWrapper = Wrappers.query();

        if (deptNum != null) {
            queryWrapper.like("t_number", deptNum);
        }

        if (deptName != null) {
            queryWrapper.like("ar_name", deptName);
        }

        iPage = deptMapper.selectPage(iPage, queryWrapper);

        PageResult<Dept> pageResult = new PageResult<>();
        pageResult.setTotal(iPage.getTotal());
        pageResult.setTotalPage(iPage.getPages());
        pageResult.setItems(iPage.getRecords());

        return pageResult;
    }

    @Override
    public void insertDept(Dept dept) {
        try {
            dept.setSStatus(0);

            deptMapper.insert(dept);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CfException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Override
    public Dept findByDeptId(Long id) {
        Dept dept = deptMapper.selectById(id);
        if (dept == null) {
            throw new CfException(ExceptionEnum.FIND_OPERATION_FAIL);
        }
        return dept;
    }

    @Override
    public void updateDept(Dept dept) {
        try {
            deptMapper.updateById(dept);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CfException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }

    }

    @Override
    public List<Dept> findAll() {

        return deptMapper.selectList(null);
    }

    @Override
    public void delete(Long id) {
        Emp emp = new Emp();
        emp.setDeptId(id);
        QueryWrapper queryWrapper = Wrappers.query(emp);

        Integer count = empMapper.selectCount(queryWrapper);
        if (count > 1) {
            throw new CfException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
        deptMapper.deleteById(id);
    }
}
